<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索结果 - 内容评级平台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        /* 全局样式 - 与主界面完全一致 */
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4cc9f0;
            --dark: #1d3557;
            --darker: #0d1b2a;
            --light: #f8f9fa;
            --gray: #8d99ae;
            --success: #4caf50;
            --warning: #ff9800;
            --danger: #f44336;
            --card-bg: rgba(29, 53, 87, 0.8);
            --transition: all 0.3s ease;
        }

        body {
            background: linear-gradient(135deg, var(--darker) 0%, var(--dark) 100%);
            color: var(--light);
            min-height: 100vh;
            background-attachment: fixed;
            line-height: 1.6;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
        }

        /* 结果容器 - 与主界面一致 */
        .results-container {
            max-width: 1400px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        /* 结果头部 */
        .results-header {
            margin-bottom: 3rem;
            text-align: center;
        }

        .results-title {
            font-size: 2.5rem;
            color: var(--accent);
            margin-bottom: 1rem;
            position: relative;
            display: inline-block;
        }

        .results-title::after {
            content: "";
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 100%;
            height: 3px;
            background: var(--accent);
            border-radius: 3px;
        }

        .results-count {
            font-size: 1.2rem;
            color: var(--gray);
        }

        /* 结果网格 - 使用主界面卡片样式 */
        .results-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 2rem;
        }

        .result-card {
            background: var(--card-bg);
            border-radius: 15px;
            overflow: hidden;
            transition: var(--transition);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            backdrop-filter: blur(5px);
        }

        .result-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
        }

        .result-image {
            width: 100%;
            height: 250px;
            object-fit: cover;
        }

        .result-info {
            padding: 1.5rem;
        }

        .result-type {
            display: inline-block;
            padding: 0.3rem 0.8rem;
            background: rgba(67, 97, 238, 0.3);
            color: var(--accent);
            border-radius: 30px;
            font-size: 0.9rem;
            font-weight: 500;
            margin-bottom: 0.8rem;
        }

        .result-title {
            font-size: 1.4rem;
            margin: 0.5rem 0;
            color: var(--light);
            display: -webkit-box;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .result-meta {
            font-size: 0.95rem;
            color: var(--gray);
            margin-bottom: 1rem;
            display: -webkit-box;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .result-rating {
            display: flex;
            align-items: center;
            gap: 0.8rem;
            margin-bottom: 1.5rem;
        }

        .stars {
            color: var(--warning);
            display: flex;
            gap: 2px;
        }

        .rating-value {
            font-weight: 600;
        }

        .result-description {
            font-size: 0.95rem;
            line-height: 1.6;
            color: var(--gray);
            margin-bottom: 1.5rem;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        /* 按钮样式 - 与主界面完全一致 */
        .btn {
            display: inline-flex;
            align-items: center;
            padding: 0.7rem 1.5rem;
            border-radius: 30px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: var(--transition);
            border: none;
            text-decoration: none;
        }

        .btn-primary {
            background: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background: var(--secondary);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(67, 97, 238, 0.4);
        }

        .btn-outline {
            background: transparent;
            border: 2px solid var(--primary);
            color: var(--primary);
        }

        .btn-outline:hover {
            background: rgba(67, 97, 238, 0.1);
            transform: translateY(-2px);
        }

        .result-actions {
            display: flex;
            gap: 1rem;
        }

        /* 无结果样式 */
        .no-results {
            text-align: center;
            padding: 3rem;
            grid-column: 1 / -1;
            background: var(--card-bg);
            border-radius: 15px;
        }

        .no-results i {
            font-size: 3rem;
            color: var(--gray);
            margin-bottom: 1.5rem;
        }

        .no-results p {
            font-size: 1.2rem;
            color: var(--light);
            margin-bottom: 0.5rem;
        }

        /* 分页样式 - 与主界面一致 */
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 3rem;
            gap: 0.5rem;
        }

        .pagination-item {
            padding: 0.7rem 1.2rem;
            border-radius: 8px;
            background: var(--card-bg);
            color: var(--light);
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            justify-content: center;
            min-width: 40px;
        }

        .pagination-item:hover {
            background: rgba(67, 97, 238, 0.3);
            color: var(--accent);
        }

        .pagination-item.active {
            background: var(--primary);
            color: white;
        }

        /* 响应式设计 - 与主界面一致 */
        @media (max-width: 1200px) {
            .results-grid {
                grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            }
        }

        @media (max-width: 768px) {
            .results-container {
                padding: 0 1rem;
            }

            .results-title {
                font-size: 2rem;
            }

            .result-actions {
                flex-direction: column;
            }

            .btn {
                width: 100%;
                justify-content: center;
            }
        }

        @media (max-width: 480px) {
            .results-grid {
                grid-template-columns: 1fr;
            }

            .results-title {
                font-size: 1.8rem;
            }
        }
    </style>
</head>
<body>
<!-- 包含导航栏 -->
<%@ include file="navbar.jsp" %>

<div class="results-container">
    <div class="results-header">
        <h1 class="results-title">搜索: "${param.query}"</h1>
        <p class="results-count">找到 ${totalResults} 个结果</p>
    </div>

    <div class="results-grid">
        <c:choose>
            <c:when test="${not empty searchResults}">
                <c:forEach items="${searchResults}" var="result">
                    <div class="result-card">

                        <div class="result-info">
                            <span class="result-type">
                                <c:choose>
                                    <c:when test="${result.type == 'movie'}">电影</c:when>
                                    <c:when test="${result.type == 'book'}">图书</c:when>
                                    <c:when test="${result.type == 'music'}">音乐</c:when>
                                </c:choose>
                            </span>
                            <h3 class="result-title">${result.title}</h3>
                            <div class="result-meta">
                                <c:if test="${not empty result.author}">${result.author} · </c:if>
                                <c:if test="${not empty result.year}">${result.year}</c:if>
                            </div>
                            <div class="result-rating">
                                <div class="stars">
                                    <c:forEach begin="1" end="5" var="i">
                                        <c:choose>
                                            <c:when test="${i <= result.rating}">
                                                <i class="fas fa-star"></i>
                                            </c:when>
                                            <c:when test="${i - 0.5 <= result.rating && result.rating < i}">
                                                <i class="fas fa-star-half-alt"></i>
                                            </c:when>
                                            <c:otherwise>
                                                <i class="far fa-star"></i>
                                            </c:otherwise>
                                        </c:choose>
                                    </c:forEach>
                                </div>
                                <div class="rating-value">${result.rating}</div>
                            </div>
                            <p class="result-description">${result.description}</p>
                            <div class="result-actions">
                                <a href="${pageContext.request.contextPath}/content/detail?type=${result.type}&id=${result.id}"
                                   class="btn btn-primary">
                                    <i class="fas fa-info-circle"></i> 查看详情
                                </a>
                                <button class="btn btn-outline" onclick="addToFavorites('${result.id}', '${result.type}')">
                                    <i class="far fa-heart"></i> 收藏
                                </button>
                            </div>
                        </div>
                    </div>
                </c:forEach>
            </c:when>
            <c:otherwise>
                <div class="no-results">
                    <i class="fas fa-search"></i>
                    <p>没有找到与"${param.query}"相关的内容</p>
                    <p>尝试使用不同的关键词或筛选条件</p>
                </div>
            </c:otherwise>
        </c:choose>
    </div>

    <c:if test="${totalPages > 1}">
        <div class="pagination">
            <c:if test="${currentPage > 1}">
                <a href="search?query=${param.query}&type=${param.type}&page=${currentPage - 1}" class="pagination-item">
                    <i class="fas fa-chevron-left"></i>
                </a>
            </c:if>

            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == currentPage}">
                        <span class="pagination-item active">${i}</span>
                    </c:when>
                    <c:otherwise>
                        <a href="search?query=${param.query}&type=${param.type}&page=${i}" class="pagination-item">${i}</a>
                    </c:otherwise>
                </c:choose>
            </c:forEach>

            <c:if test="${currentPage < totalPages}">
                <a href="search?query=${param.query}&type=${param.type}&page=${currentPage + 1}" class="pagination-item">
                    <i class="fas fa-chevron-right"></i>
                </a>
            </c:if>
        </div>
    </c:if>
</div>

<!-- 包含页脚 -->
<%@ include file="footer.jsp" %>

<script>
    function addToFavorites(itemId, itemType) {
        fetch('${pageContext.request.contextPath}/favorite', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                itemId: itemId,
                itemType: itemType
            })
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('已添加到收藏夹');
                } else {
                    alert(data.message || '收藏失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('收藏过程中发生错误');
            });
    }
</script>
</body>
</html>